package edu.gxmzu.task.service.impl;
import edu.gxmzu.task.entity.Attachments;
import edu.gxmzu.task.entity.Works;
import edu.gxmzu.task.mapper.AttachmentsMapper;
import edu.gxmzu.task.mapper.TaskMapper;
import edu.gxmzu.task.params.NewTaskParam;
import edu.gxmzu.task.service.TaskService;
import edu.gxmzu.task.utils.FileUtil;
import edu.gxmzu.task.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static edu.gxmzu.task.config.FilePathConfig.attachmentPath;

@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    TaskMapper taskMapper;
    @Resource
    private AttachmentsMapper attachmentsMapper;

    @Override
    public int deleteTask(List<String> ids) {
        return taskMapper.deleteTask(ids);
    }

    @Override
    public Result newTask(NewTaskParam workParam, String userId) throws IOException {
        if (workParam.getTitle().isEmpty()) return Result.badRequest("任务名称不能为空");
        if (workParam.getContent().isEmpty()) return Result.badRequest("任务内容不能为空");
        if (workParam.getEndTime()==null) return Result.badRequest("结束时间不能为空");

        Works work=new Works();  //实例化表实体
        if (workParam.getFile()!=null){
            // 获取文件名
            String fileName = workParam.getFile().getOriginalFilename();
            // 获取文件类型
            String fileType = workParam.getFile().getContentType();
            // 获取文件大小
            long fileSize = workParam.getFile().getSize();

            // 获取文件的哈希值（MD5）
            String fileHash;
            // 计算文件的 MD5 值
            byte[] fileBytes = workParam.getFile().getBytes();
            fileHash=DigestUtils.md5DigestAsHex(fileBytes);

//        最终文件名
            String newFileName=System.currentTimeMillis()+"_"+fileName;

            String projectPath = System.getProperty("user.dir");
            String filesPath = projectPath + File.separator + "WEB-INF" + File.separator + "files";
            String filesRealPath = filesPath + File.separator;
            System.out.println(filesRealPath);
            // 保存文件到指定路径
            File directory = new File(filesRealPath);
            if (!directory.exists()) {
                directory.mkdirs(); // 确保文件夹存在
            }
            // 创建新文件路径
            File newFile = new File(filesRealPath + newFileName);

            // 将上传的文件保存到新位置
            workParam.getFile().transferTo(newFile);
//        插入到到附件表
            Attachments attachments=new Attachments();
            attachments.setName(newFileName);
            attachments.setType(fileType);
            attachments.setSize(fileSize);
            attachments.setHash(fileHash);
            attachments.setPath(newFileName);
            if (attachmentsMapper.insert(attachments)!=1) return Result.badRequest("上传附件失败");
            work.setAttachmentsId(attachments.getId());
        }

//        任务表插入

        BeanUtils.copyProperties(workParam,work);
        work.setUserId(userId);
        work.setWorkStatusId(0);
        work.setPriorityId(0);
        work.setCreateTime(String.valueOf(System.currentTimeMillis()));
        work.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        int row=taskMapper.insert(work);
        if (row>0) return Result.ok("新建任务成功");
        return Result.fail("新建任务失败");
    }


}